﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.View;
using System.Collections.Generic;
using nyxwin.Main.Base.ViewModels;
using nyxwin.Main.View.Main;
using Microsoft.Phone.Shell;
using System.Linq;

namespace nyxwin.Main.ViewModel
{
    public class MainPageViewModel : BaseViewModel
    {
        #region Fields
        private PivotItemView[] _pivotItems;
        private Dictionary<string, PivotItemView> _pivotIds;
        private ApplicationBarMenuItem _settingsMenuItem;
        private ApplicationBarMenuItem _aboutMenuItem;
        private List<KeyValuePair<ApplicationBarIconButton, AppBarButton>> _currentButtons;
        private System.ComponentModel.PropertyChangedEventHandler _buttonChangedHandler;  
        #endregion

        #region Construction
        public MainPageViewModel()
        {
            _pivotItems = new PivotItemView[]
            {
                new MailView(),
                new FriendsView(),
                new BookMarkView()
            };

            _pivotIds = new Dictionary<string, PivotItemView>();
            _pivotIds.Add("mail", _pivotItems[0]);
            _pivotIds.Add("friends", _pivotItems[1]);
            _pivotIds.Add("bookmarks", _pivotItems[2]);

            _settingsMenuItem = new ApplicationBarMenuItem();
            _settingsMenuItem.Click += new EventHandler(SettingsClickAction);
            _aboutMenuItem = new ApplicationBarMenuItem();
            _aboutMenuItem.Click += new EventHandler(AboutClickAction);

            _currentButtons = new List<KeyValuePair<ApplicationBarIconButton, AppBarButton>>();
            _buttonChangedHandler = new System.ComponentModel.PropertyChangedEventHandler(appBarButton_PropertyChanged);
            SetAppBarMenuItemsTitle();
        } 
        #endregion

        #region Localization changed
        protected override void OnLocalizationChanged()
        {
            base.OnLocalizationChanged();

            SetAppBarMenuItemsTitle();
        } 
        #endregion

        #region Pivots
        public IEnumerable<PivotItemView> Pivots
        {
            get { return _pivotItems; }
        } 
        #endregion

        #region AppBar
        private void SetAppBarMenuItemsTitle()
        {
            _settingsMenuItem.Text = Localization.GetString("Settings");
            _aboutMenuItem.Text = Localization.GetString("About");
        }

        void AboutClickAction(object sender, EventArgs e)
        {
            // TODO: Add about click handler
        }

        void SettingsClickAction(object sender, EventArgs e)
        {
            // TODO: Add settings click handler
        }

        public IEnumerable<ApplicationBarMenuItem> AppBarMenuItems
        {
            get
            {
                yield return _settingsMenuItem;
                yield return _aboutMenuItem;
            }
        }

        private void InitButtons(PivotItemView pivotContentView)
        {
            foreach (var oldButton in _currentButtons.Select(x => x.Value))
            {
                oldButton.PropertyChanged -= _buttonChangedHandler;
            }

            _currentButtons.Clear();

            foreach (var newButton in pivotContentView.PivotViewModel.AppBarButtons)
            {
                ApplicationBarIconButton abib = new ApplicationBarIconButton();
                abib.Text = newButton.Title;
                abib.IsEnabled = newButton.IsEnabled;
                abib.IconUri = newButton.Uri;
                abib.Click += new EventHandler(appBarButtonClick);
                newButton.PropertyChanged += _buttonChangedHandler;
                _currentButtons.Add(new KeyValuePair<ApplicationBarIconButton, AppBarButton>(abib, newButton));
            }

            RaisePropertyChanged(() => AppBarButtons);
        }

        void appBarButtonClick(object sender, EventArgs e)
        {
            var button = sender as ApplicationBarIconButton;
            var bckButton = _currentButtons.Where(x => x.Key == button).Select(x => x.Value).FirstOrDefault();

            if (bckButton != null)
            {
                bckButton.Click();
            }
        }

        public IEnumerable<ApplicationBarIconButton> AppBarButtons
        {
            get
            {
                return _currentButtons.Select(x => x.Key);
            }
        }

        void appBarButton_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var data = sender as AppBarButton;

            if (data == null)
                return;

            var button = _currentButtons.Where(x => x.Value == sender).Select(x => x.Key).FirstOrDefault();

            if (button == null)
                return;

            if (e.PropertyName == "Title")
            {
                button.Text = data.Title;
            }
            else if (e.PropertyName == "IsEnabled")
            {
                button.IsEnabled = data.IsEnabled;
            }
        }
        #endregion

        #region Changing Pivot View actions
        public void Enter(PivotItemView pivotContentView)
        {
            InitButtons(pivotContentView);

            pivotContentView.Enter();
        }

        internal void Leave(PivotItemView pivotContentView)
        {
            pivotContentView.Leave();
        } 
        #endregion

        #region GetPivotItemViewFromId
        public PivotItemView GetPivotItemViewFromId(string id)
        {
            if (_pivotIds.ContainsKey(id))
            {
                return _pivotIds[id];
            }
            else
                return _pivotItems[0];
        } 
        #endregion
    }
}
